// $Id: AbstractWorkitemListenerView.java 48 2011-10-17 18:20:21Z huijzer@gmail.com $
package lime.workflow.view;

import lime.model.Workitem;
import lime.service.SpringApplicationContext;
import lime.service.WorkflowService;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.ViewPart;

public abstract class AbstractWorkitemListenerView extends ViewPart {

	// Services
	private WorkflowService workflowService;

	@Override
	public void init(IViewSite site) throws PartInitException {
		
		super.init(site);

		workflowService = (WorkflowService) SpringApplicationContext.getBean("workflowService");

		getSite().getPage().addSelectionListener(new ISelectionListener() {
			@Override
			public void selectionChanged(IWorkbenchPart part, ISelection selection) {
				if (selection != null && selection instanceof IStructuredSelection) {
					IStructuredSelection structuredSelection = (IStructuredSelection) selection;
					Object element = structuredSelection.getFirstElement();
					if (element != null && element instanceof Workitem) {
						Workitem workitem = (Workitem) element;
						update(workitem);
					}
				}
			}
		});
	}

	protected abstract void update(Workitem workitem);

	public WorkflowService getWorkflowService() {
		return workflowService;
	}

}
